package com.bio.service.user.impl;

import java.util.List;

import com.bio.dao.user.PositionDao;
import com.bio.dao.user.UserDAO;
import com.bio.entity.user.Position;
import com.bio.service.impl.AbstractService;
import com.bio.service.user.PositionService;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;

public class PositionServiceImpl extends AbstractService<Position, PositionDao> implements PositionService{

	public PositionServiceImpl(PositionDao dao) {
		super(dao);
	}
	
	private UserDAO userDAO;

	@Override
	public Pager<Position> findPagerByCompanyId(Pagination pagination,
			Long companyId) {
		return dao.findPagerByCompanyId(pagination, companyId);
	}

	@Override
	public List<Position> findAllByCompanyId(Long companyId) {
		return dao.findAllByCompanyId(companyId);
	}

	@Override
	public void removePositionsBySuperior(Long superiorId) {
		dao.removePositionsBySuperior(superiorId);
	}

	@Override
	public void remove(Long positionId) {
		this.recursionDelete(positionId);
	}
	
	private void recursionDelete(Long positionId){
		List<Position> positions = this.dao.findAllBySuperior(positionId);
		if(positions.size() > 0){
			for(Position p : positions){
				recursionDelete(p.getId());
			}
		}
		
		userDAO.removeUsersByPosition(positionId);
		this.dao.remove(positionId);
		
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}
